package input;

import java.util.HashMap;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Mouse;

/**
 * Recebe todos os inputs e manda para o input manager.
 * Nen public nen private, vista apenas no pacote.
 * @author Ygor
 *
 */
class MouseListener implements IInputListener {

	HashMap<Integer, Key> mappings;
	
	MouseListener() {
		mappings = new HashMap<Integer, Key>();
		
		mappings.put(0, InputManager.mouse.leftButton);
		mappings.put(2, InputManager.mouse.middleButton);
		mappings.put(1, InputManager.mouse.rightButton);
	}
	
	@Override
	public void pool() {
		Mouse.poll();
		
		while (Mouse.next()) {
			Key target = mappings.get(Mouse.getEventButton());
			if (null != target) {
				target.toogle(Mouse.getEventButtonState());
				
				// debug
				if (Mouse.getEventButton() == 0 && Mouse.getEventButtonState()) {
					System.out.println("Esquerdo: " + Mouse.getX() + ", " + Mouse.getY());
				} else if (Mouse.getEventButton() == 2 && Mouse.getEventButtonState()) {
					System.out.println("Meio: " + Mouse.getX() + ", " + Mouse.getY());
				} else if (Mouse.getEventButton() == 1 && Mouse.getEventButtonState()) {
					System.out.println("Direito: " + Mouse.getX() + ", " + Mouse.getY());
				}
			}
		}
	}

	@Override
	public void create() {
		try {
			org.lwjgl.input.Mouse.create();
		} catch (LWJGLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void destroy() {
		org.lwjgl.input.Mouse.destroy();
	}
}
